﻿<template>
  <a-drawer
    :confirmLoading="confirmLoading"
    :title="title"
    :visible="visible"
    width="40%"
    @close="
      () => {
        this.visible = false
      }
    "
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="entity" :rules="rules" v-bind="layout">
        <a-form-model-item label="APP名称" prop="Name">
          <a-input v-model="entity.Name" autocomplete="off" :disabled="true" style="background-color: rgba(0,0,0,0.1);color: #000c17;font-weight: bold"/>
        </a-form-model-item>
        <a-form-model-item label="版本号" prop="VersionNumber">
          <a-input v-model="entity.VersionNumber" autocomplete="off" @change="BindAppName"/>
        </a-form-model-item>
        <a-form-model-item label="强制更新" prop="ForceUpdates">
          <a-radio-group v-model="entity.ForceUpdates" button-style="solid" :default-value="0">
            <a-radio :value="1">是</a-radio>
            <a-radio :value="0">否</a-radio>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item label="类型" prop="Type">
          <a-radio-group v-model="entity.Type" button-style="solid" @change="AppTypeSelect">
            <a-radio value="Android">Android</a-radio>
            <a-radio value="IOS">IOS</a-radio>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item label="APP安装包" prop="Url">
          <c-upload-file v-if="AppFileUrlShow" v-model="entity.Url" :maxCount="1"></c-upload-file>
          <a-input v-if="!AppFileUrlShow" v-model="entity.Url" autocomplete="off" type="textarea" placeholder="请输入IOS安装地址" :rows="4"/>
        </a-form-model-item>
        <a-form-model-item label="版本简要" prop="Remark">
          <a-input v-model="entity.Remark" autocomplete="off" type="textarea" :rows="4"/>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
    <div
      :style="{
        position: 'absolute',
        right: 0,
        bottom: 0,
        width: '100%',
        borderTop: '1px solid #e9e9e9',
        padding: '10px 16px',
        background: '#fff',
        textAlign: 'right',
        zIndex: 1
      }"
    >
      <a-button
        :style="{ marginRight: '8px' }"
        @click="
          () => {
            this.visible = false
          }
        "
      >取消
      </a-button
      >
      <a-button type="primary" @click="handleSubmit">保存</a-button>
    </div>
  </a-drawer>
</template>

<script>
import CUploadFile from './CUploadFile'

export default {
  props: {
    afterSubmit: {
      type: Function,
      default: null
    }
  },
  components: {
    CUploadFile
  },
  data () {
    return {
      layout: {
        labelCol: { span: 5 },
        wrapperCol: { span: 18 }
      },
      visible: false,
      TypeList: [],
      AppFileUrlShow: true,
      confirmLoading: false,
      listfalg: true,
      entity: {
        Type: 'Android'
      },
      ParentIdTreeData: [],
      rules: {
        Url: [{ required: true, message: '请上传安装包或者输入安装地址' }],
        VersionNumber: [{ required: true, message: '请填写版本号' }],
        Remark: [{ required: true, message: '请填写版本简要' }]
      },
      title: ''
    }
  },
  methods: {
    init () {
      this.visible = true
      this.entity = {
        Url: '',
        Type: 'Android',
        ForceUpdates: 0
      }
      this.BindAppName()
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    },
    openDrawer (id, title) {
      this.title = title
      this.init()
      if (id) {
        this.loading = true
        this.$http.post('/ApkEdition/Apk_Edition/GetTheData', { id: id }).then(resJson => {
          this.loading = false
          this.entity = resJson.Data
          if (this.entity.Type === 'Android') {
            this.AppFileUrlShow = true
          } else if (this.entity.Type === 'IOS') {
            this.AppFileUrlShow = false
          }
        })
      }
    },
    AppTypeSelect (e) {
      if (e && e.target) {
        if (e.target.value === 'Android') {
          this.AppFileUrlShow = true
          this.entity.Url = ''
        } else if (e.target.value === 'IOS') {
          this.AppFileUrlShow = false
          this.entity.Url = 'https://apps.apple.com/cn/app/%E5%B7%A1%E6%A3%80go/id6450889428'
        }
        this.BindAppName()
      }
    },
    BindAppName () {
      this.entity.Name = 'XunJianGo_v' + this.entity.VersionNumber + '_' + this.timeFormat(new Date(), 'YYYYMMDD')
    },
    handleSubmit () {
      this.$refs['form'].validate(valid => {
        if (!valid) {
          return
        }
        this.confirmLoading = true
        // console.log(this.entity.Url)
        console.log(this.entity.Type)
        this.$http.post('/ApkEdition/Apk_Edition/SaveData', this.entity).then(resJson => {
          this.confirmLoading = false
          if (resJson.Success) {
            this.$message.success('操作成功!')
            this.afterSubmit()
            this.visible = false
          } else {
            this.$message.error(resJson.Msg)
          }
        })
      })
    }
  }
}
</script>
